<?php

/**
 * Description of I_Model
 * 
 * @package Core
 * @subpackage Models
 * @author Carlos Carvalho
 */
class I_Model extends CI_Model {

    protected $_table_name = '';
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_order_by = '';
    protected $_direction = 'ASC';
    public $rules = array();
    protected $_timestamps = FALSE;
    private $errors = array();
    protected $soft_delete = FALSE;

    function __construct() {
        parent::__construct();
    }

    function get($id = null, $single = false) {
        $method = 'result';

        if ($single) {
            $method = 'row';
        }
        if (!is_null($id) && $id) {
            $filter = $this->_primary_filter;
            $id = $filter($id);

            $this->db->where($this->_primary_key, $id);
            $method = 'row';
        }

        if ($this->_order_by != '') {

            $this->db->order_by($this->_order_by, $this->_direction);
        }

        if (isset($this->_method)) {
            $method = $this->_method;
        }
        if ($this->soft_delete !== FALSE) {
            $this->db->where('DELETED_AT', NULL);
        }


        $rs = $this->db->get($this->_table_name)->$method();

        if (!$rs) {
            $this->log_errors();
            return false;
        }

        return $rs;
    }

    function get_by($where, $single = false) {
        $this->db->where($where);
        return $this->get(null, $single);
    }

    function save($data, $id = null) {

        //
        if ($this->_timestamps) {
            $now = date('Y-m-d H:i:s');
            $id || $data['CRIADO'] = $now;
            $data['ALTERADO'] = $now;
        }

        if ($id === null) {

            !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
            $this->db->set($data);
            $this->db->insert($this->_table_name);
            $id = $this->db->insert_id();
        } else {
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->set($data);
            $this->db->where($this->_primary_key, $id);
            $this->db->update($this->_table_name);
        }
        if (!$id) {
            $this->log_errors();
            return false;
        }

        return $id;
    }

    function delete($id) {
        if ($this->soft_delete) {
            return $this->save(array('DELETED_AT' => date('Y-m-d H:i:s')), $id);
        }
        $filter = $this->_primary_filter;
        if (!is_array($id)) {
            $id = $filter($id);
            if (!$id) {
                return false;
            }
            $this->db->where($this->_primary_key, $id);
        }

        if (is_array($id)) {
            $this->db->where($id);
        }

        $this->db->limit(1);
        $delete = $this->db->delete($this->_table_name);
        if (!$delete) {
            $this->log_errors();
            return false;
        }
        return true;
    }

    function array_from_post($fields) {
        $data = array();
        return;
        foreach ($fields as $key) {
            $data[$key] = $this->input->post($key);
        }
        return $data;
    }

    function count($where = array()) {

        if (count($where) > 0) {
            $this->db->where($where);
        }
        $this->db->from($this->_table_name);
        return $this->db->count_all_results();
    }

    function num_rows($id = null) {
        $this->_method = 'num_rows';
        $num_rows = $this->get($id);
        unset($this->_method);
        return (int) $num_rows;
    }

    function get_log_errors($index = null, $string = true) {
        if (isset($this->errors[$index])) {
            return $this->errors[$index];
        }
        return $string == true ? implode('<br/>', $this->errors) : $this->errors;
    }

    function log_errors() {
        $this->errors = array('number' => '<br/>' . Utils::message('error_mysql_number') . $this->db->_error_number(), 'message' => $this->db->_error_message());
    }

}
